﻿Imports System.Text.RegularExpressions
Imports System.Text.Encoding
Imports System.Net
Imports System.Threading

Public Class Form1
    Dim strHtmlText As String

    Dim bla As Boolean

    Function ReadHTML(ByVal url As String) As String
        Try
            Cursor = Cursors.WaitCursor


            Dim wc As New WebClient
            Dim res As String = UTF8.GetString(wc.DownloadData(url))
            Cursor = Cursors.Default
            Return res
        Catch ex As WebException

            lstLinkList.Items.Clear()
            MessageBox.Show(ex.Message, "Fehler bei der Verbindung", _
               MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Cursor = Cursors.Default
            Return ""
        End Try
    End Function

    Sub FindHrefs(ByVal inputString As String)

        Dim r As Regex
        Dim m As Match
        Dim regString As String = _
            "href\s*=\s*(?:""(?<1>[^""]*)""|(?<1>\S+))"

        r = New Regex(regString, _
            RegexOptions.IgnoreCase Or RegexOptions.Compiled)
        m = r.Match(inputString)
        lstLinkList.Items.Clear()

        While m.Success
            If m.Groups(1).Value.Contains("/n/") Then

                lstLinkList.Items.Add(m.Groups(1).Value)
                Dim str As String = Replace(m.Groups(1).Value, "/n/", "")
                Dim str2 As String = Replace(str, "/", "")

                If ListBox1.Items.Contains(str2) Then
                Else
                    ListBox1.Items.Add(str2)
                End If

            End If
            m = m.NextMatch()
        End While
    End Sub




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(TextBox1.Text)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        While Label5.Text <= TextBox5.Text


            Me.WebBrowser1.Navigate("http://web.stagram.com/tag/" & Me.TextBox4.Text & "/")
            For i = 1 To 100
                Thread.Sleep(10)
                Application.DoEvents()
            Next
            While bla = False
                Application.DoEvents()
            End While

            Dim x As Integer = WebBrowser1.Document.GetElementsByTagName("img").Count
            Dim str As String
            For i = 0 To x - 1
                str = WebBrowser1.Document.GetElementsByTagName("img").Item(i).OuterHtml.ToString()
                If InStr(str, "img/like.png") Then
                    WebBrowser1.Document.GetElementsByTagName("img").Item(i).InvokeMember("click")
                    Label5.Text = Label5.Text + 1
                    For i2 = 1 To 10
                        Thread.Sleep(10)
                        Application.DoEvents()
                    Next
                End If
            Next
     End While





    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MsgBox("Es gibt noch ein paar Probleme mit diesem Feature! " & vbNewLine & "Fehler über dieses Feature bitte NICHT melden!")
        Me.ListBox1.Items.Clear()
        Application.DoEvents()

        FindHrefs(ReadHTML("http://web.stagram.com/tag/" & Me.TextBox3.Text))


        For gg = 0 To TextBox2.Text - 1
            Me.ListBox1.SelectedIndex = gg

            Me.WebBrowser1.Navigate("http://web.stagram.com/n/" & Me.ListBox1.Text & "/")
            For i = 1 To 100
                Thread.Sleep(10)
                Application.DoEvents()
            Next
            While bla = False
                Application.DoEvents()
            End While
            bla = False

            Dim x As Integer = Me.WebBrowser1.Document.GetElementsByTagName("span").Count
            Dim str As String
            For i = 0 To x - 1
                str = Me.WebBrowser1.Document.GetElementsByTagName("span").Item(i).OuterHtml.ToString()

                If InStr(str, ">Follow<") Then
                    Me.WebBrowser1.Document.GetElementsByTagName("span").Item(i).InvokeMember("click")
                End If
            Next
            Me.Label6.Text = Label6.Text + 1


            If Me.ListBox1.SelectedIndex >= Me.ListBox1.Items.Count - 3 Then
                Me.ListBox1.Items.Clear()
                FindHrefs(ReadHTML("http://web.stagram.com/tag/" & Me.TextBox3.Text))
            End If

            
        Next

    End Sub






    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        ToolStripStatusLabel2.Text = "Finish Loading"
        bla = True

    End Sub


    Private Sub WebBrowser1_Navigating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating

        ToolStripStatusLabel2.Text = "Loading..."
        bla = False
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bla = True
    End Sub
End Class
